package com.woniu.p2p.behindController;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.PersonManagerService;
import com.woniu.p2p.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * manageController
 *
 *人员管理
 */
@Controller
@RequestMapping("/manage")
public class PersonManagerController {

    @Autowired
    PersonManagerService personManagerService;


    //查询管理员
    @RequestMapping("/getAllManage")
    public String getAllManage(Map map,Integer status,String name,Integer pageNum){
        Integer start=pageNum==null?1:pageNum;
        PageInfo<Admin> admins = personManagerService.findAdminByStatusAndName(status, name,start);
        map.put("admin",admins);
        map.put("name",name);
        map.put("status",status);
        return "content/personManager";
    }

    //改变管理员状态
    @RequestMapping("/changeStatus")
    public String changeStatus(Integer adminId){

        Admin admin = personManagerService.selectByAdminId(adminId);
        System.out.println(admin.getAdminStatus());
       if(admin.getAdminStatus()==1) {
           admin.setAdminStatus(0);
       }else if(admin.getAdminStatus()==0){
           admin.setAdminStatus(1);
       }
       personManagerService.updateAdmin(admin);

        return "redirect:/manage/getAllManage";
    }

     //添加管理员
    @RequestMapping("/addAdmin")
    public String addAdmin(String name,String pass){
        Admin admin=new Admin();
        admin.setAdminAccount(name);
        admin.setAdminPassword(pass);
        admin.setAdminStatus(0);
        personManagerService.addAdmin(admin);
        return "redirect:/manage/getAllManage";
    }

    //批量删除管理员
    @RequestMapping("/delAdmin")
    public String delAdmin(String ids){
        String[] id = ids.split(",");
        for (String aid : id) {
            Integer adminId=Integer.parseInt(aid);
            personManagerService.delAdmin(adminId);

        }
        return "redirect:/manage/getAllManage";
    }
}
